//
//  TheWorldAddressTableViewController.m
//  Tourism
//
//  Created by 鲁万玺 on 2017/8/16.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "TheWorldAddressTableViewController.h"
#import "TravelDateViewController.h"
@interface TheWorldAddressTableViewController ()

@end

@implementation TheWorldAddressTableViewController

+ (instancetype)initWithBlock:(backCity)block
{
    TheWorldAddressTableViewController *vc = [[TheWorldAddressTableViewController alloc]init];
    vc.backBlock = block;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"选择城市";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui:)];
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    
    if (self.data == nil)
    {
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Loaction" ofType:@"plist"];
        NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path1][@"Location"];
        
        self.data = dic[@"Continent"];
    }
}

- (NSString *)allStr
{
    if (_allStr == nil)
    {
        _allStr = @"";
    }
    return _allStr;
}

- (NSString *)chooseStr
{
    if (_chooseStr == nil)
    {
        _chooseStr = @"";
    }
    return _chooseStr;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    cell.textLabel.font = DEF_FontSize_15;
    cell.textLabel.text = self.data[indexPath.row][@"Name"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60/WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TheWorldAddressTableViewController *vc = [[TheWorldAddressTableViewController alloc]init];
    
    //选择州
    if ([self.data[indexPath.row][@"CountryRegion"] isKindOfClass:[NSArray class]])
    {
        vc.data = self.data[indexPath.row][@"CountryRegion"];
    }
    else if([self.data[indexPath.row][@"CountryRegion"] isKindOfClass:[NSDictionary class]])
    {
        vc.data = @[@{@"Name":self.data[indexPath.row][@"CountryRegion"][@"Name"]}];
    }
    
    //选择国家
    else if ([self.data[indexPath.row][@"State"] isKindOfClass:[NSArray class]])
    {
        vc.data = self.data[indexPath.row][@"State"];
    }
    else if ([self.data[indexPath.row][@"State"] isKindOfClass:[NSDictionary class]])
    {
        vc.data = self.data[indexPath.row][@"State"][@"City"];
    }
    
    //选择省
    else if([self.data[indexPath.row][@"City"] isKindOfClass:[NSArray class]])
    {
        vc.data = self.data[indexPath.row][@"City"];
    }
    
    //选择结束
    else
    {
        NSArray *vcArr = self.navigationController.viewControllers;
        for (NSInteger i = vcArr.count-1; i>=0; i--)
        {
            if ([vcArr[i] isKindOfClass:[TheWorldAddressTableViewController class]])
            {
                continue;
            }
            else
            {
                self.chooseStr = self.data[indexPath.row][@"Name"];
                NSString *str = [NSString stringWithFormat:@"%@%@",self.allStr,self.chooseStr];
                __weak typeof(TheWorldAddressTableViewController *) weakVC = vcArr[i+1];
                weakVC.backBlock(str);
                [self.navigationController popToViewController:vcArr[i] animated:YES];
                break;
            }
        }
        return;
    }
    
    NSRange range = [self.allStr rangeOfString:self.chooseStr];
    if (range.location != NSNotFound)
    {
        NSMutableString *mutableString = [[NSMutableString alloc]initWithString:self.allStr];
        [mutableString deleteCharactersInRange:range];
        self.allStr = mutableString;
    }
    self.chooseStr = self.data[indexPath.row][@"Name"];
    vc.allStr = [NSString stringWithFormat:@"%@%@",self.allStr,self.chooseStr];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - click
- (void)fanhui:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
